<?php
namespace api\controllers;

use common\models\Active;
use common\models\ActiveAttr;
//use common\models\Activity;
//use common\models\ActivityAttr;
use common\models\Goods;
use common\models\Googs;
use common\models\MoneyChange;
use common\service\CommonService;
use common\service\LogisticsService;
use common\service\WechatService;
use common\service\ConstantService;
use common\service\GlobalUrlService;
use common\models\Banner;
class IndexController extends BaseController
{
    public function actionIndex(){
//        $shippercode = 'YTO';
//        $logisticcode = '889287271871822928';
//        $result = LogisticsService::getOrderTracesByJson($shippercode,$logisticcode);
//print_r($result);exit;

        return 'ok';

        $logisticcode= '889287271871822928';
        $shippercode = 'YTO';
        $result = LogisticsService::getOrderTracesByJson($shippercode,$logisticcode);
        var_dump($result);exit;

        $mch_id = \Yii::$app->params['wechat']['mchid'];
        $key = \Yii::$app->params['wechat']['key'];
        $mch_appid = \Yii::$app->params['wechat']['appid'];
        $partner_trade_no = CommonService::createOrderNo();
        $enc_bank_no = '6212263100032129509';
        $enc_true_name = '严朝政';
        $openid = 'o2zPb4oZH8l87qnvhv_b7yGxLTm4';
        $bank_code = 1002;
        $re = WechatService::pay_transfers($mch_appid,$mch_id,$partner_trade_no ,$openid,1,'测试',$key );
var_dump($re);exit;
    }


    /**
     * Banner图
     */
    public function actionBannerList(){
        $data = Banner::find()->select('id,oid,img_url,type,create_time')->where(['is_del'=>0])->orderBy(['id'=>SORT_DESC])->asArray()->all();
        foreach ($data as &$val){
            $val['img_url'] = GlobalUrlService::buildCdnUrl($val['img_url']);
        }

        return $this->renderJson($data,'',ConstantService::$response_success);
    }

    /**
     * 首页活动列表
     */
    public function actionActiveList(){
        if($this->isGet()) {
            $type = $this->get('type');
            $query = Active::find();
            $query->andWhere(['is_del' => 0]);
            $query->andWhere(['type' => $type]);
            $query->andWhere(['status' => 1]);

            $p = intval($this->get('page', 1));
            $p = ($p > 0) ? $p : 1;

            $offset = ($p - 1) * 4;
            $lists = $query->select('id,active_name,sales_volume,display_sales,goods_img,end_time,max_price,min_price')
                ->orderBy(['set_top' => SORT_DESC,'update_time' => SORT_DESC,'sales_volume' => SORT_DESC])
                ->offset($offset)
                ->limit(4)
                ->asArray()
                ->all();

            //取原价的最高价
            $aid = array_column($lists,'id');
            $attrs = ActiveAttr::find()
                ->select('max(original_price) as original_price,active_id')
                ->where(['active_id'=>$aid])
                ->groupBy('active_id')
                ->asArray()
                ->all();
            $newattr = [];
            foreach ($attrs as $aval){
                $newattr[$aval['active_id']] = $aval['original_price'];
            }
            unset($aval);

            foreach ($lists as &$val){
                $val['max_price'] = $newattr[$val['id']];
                $val['goods_img'] = GlobalUrlService::buildCdnUrl($val['goods_img']);
            }
            return $this->renderJson($lists, '', ConstantService::$response_success);
        }
        return $this->renderJson([], '参数错误', ConstantService::$response_fail);
    }

}